<?php
require_once ('IOperations.php');
require_once ('AppController.php');



/**
 * @author ValeBonus.com
 * @version 1.0
 * @created 02-dez-2013 23:36:28
 */
class InterestsController extends AppController implements IOperations {

	/**
	 * Set the controller name
	 */
	public $name = 'Interests';

	public function index() {
		// Show all elements
		$this->Interest->recursive = 0;
		$this->set('interests', $this->paginate());
	}

	public function add() {
		// Check if the request is an post http method
		if ($this->request->is('post')) {
			// Create the interest
			$this->Interest->create();
		
			// Persist the interest
			if ($this->Interest->save($this->request->data)) {
				// Show a user message
				$this->Session->setFlash(__('O interesse foi salvo'), 'flash_success');
		
				// Redirect to the index function
				$this->redirect(array('action' => 'index'));
			} else {
				// Show a user message
				$this->Session->setFlash(__('O interesse não pode ser salvo. Por favor, tente de novo'), 'flash_error');
			}
		}
	}

	/**
	 * 
	 * @param id    The Interest Id
	 */
	public function edit($id) {
		// Set the interest id
		$this->Interest->id = $id;
		
		// Check if the interest exists
		if (!($this->Interest->exists())) {
			// Throw an exception
			throw new NotFoundException(__('Interesse inválido'));
		}
		
		// Check if it is a post or a put http method
		if ($this->request->is('post') || $this->request->is('put')) {
			// Persist the interest
			if ($this->Interest->save($this->request->data)) {
				// Show a user message
				$this->Session->setFlash(__('O interesse foi salvo'), 'flash_sucess');
		
				// Redirect to the index function
				$this->redirect(array('action' => 'index'));
			} else {
				// Show a user message
				$this->Session->setFlash(__('O interesse não pode ser salvo. Por favor, tente de novo'), 'flash_error');
			}
		} else {
			// Retreive the requested data
			$this->request->data = $this->Interest->read(null, $id);
		
			// Clear the data
			//unset($this->request->data['Interest']['']);
		}
	}

	/**
	 * 
	 * @param id    The Interest Id
	 */
	public function delete($id) {
		// Check if the request is an post http method
		if (!$this->request->is('post')) {
			// Throw an exception
			throw new MethodNotAllowedException();
		}
		
		// Set the interest id
		$this->Interest->id = $id;
		
		// Check if the user exists
		if (!$this->Interest->exists()) {
			// Throw an exception
			throw new NotFoundException(__('Interesse inválido'));
		}
		
		// Delete the interest
		if ($this->Interest->delete()) {
			// Show a user message
			$this->Session->setFlash(__('Interesse apagado'), 'flash_success');
		
			// Redirect to the index function
			$this->redirect(array('action' => 'index'));
		}
		
		// Show a interest message
		$this->Session->setFlash(__('O interesse não foi apagado'), 'flash_error');
		
		// Redirect to the index function
		$this->redirect(array('action' => 'index'));
	}

	/**
	 * 
	 * @param id    The Interest Id
	 */
	public function view($id) {
		// Set the id to the interest object
		$this->Interest->id = $id;
		
		// Check if the interest exists
		if (!($this->Interest->exists())) {
			// Throw an exception
			throw new NotFoundException(__('Interesse inválido'));
		}
		
		// Read the interest object properties and show them
		$this->set('interest', $this->Interest->read(null, $id));
	}

	public function jsonlist() {
		// To disable the layout
		$this->layout = null;
		
		// Set the json content to the json object
		$this->set('json', $this->Interest->find('all'));
		
		// Call the view
		$this->render('../Elements/json');
	}
	
	/**
	 *
	 * @param id    The Interest Id
	 */
	public function jsonview($id) {
		// To disable the layout
		$this->layout = null;
	
		// Set the id to the user object
		$this->Interest->id = $id;
	
		// Check if the user exists
		if (!($this->Interest->exists())) {
			// Throw an exception
			throw new NotFoundException(__('Interesse inválido'));
		}
	
		// Set the json content to the json object
		$this->set('json', $this->Interest->read(null, $id));
	
		// Call the view
		$this->render('../Elements/json');
	}

}
?>